﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Common.ConvexHull;
using EpicBall.ScreenSystem;
using EpicBall.Camera;
using EpicBall.Levels.Classes;
using EpicBall.Levels.Classes.Enemies;

namespace EpicBall.Levels
{
    class Level2 : PhysicsGameScreen
    {
        public Level2() : base(2000, 1200) { _leveltitle = "desert"; }

        Texture2D blankTexture;
        Texture2D startTexture;
        Texture2D endTexture;
        Texture2D collectableTexture;
        Texture2D brickTexture;

        public override void LoadContent()
        {
            base.LoadContent();
            screenSong = GameStack.Content.Load<Song>("Audio/desert");
            blankTexture = GameStack.Content.Load<Texture2D>("blank");
            collectableTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/collectable");
            startTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpIn");
            endTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpOut");
            backgroundTexture = GameStack.Content.Load<Texture2D>("backgrounds/desert");

            _airResistance = new Vector2(2.5f, 0);
            _speedThreshold = 12.5f;
            gameWorld.Gravity = Vector2.UnitY * 10;

            //Start declaring level objects
            //Initiate Brick level border

            brickTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/brickwall");

            for (int i = 0; i < 13; i++)
            {
                AddObjectToWorld(new LevelObject(this, brickTexture), new Vector2(i*brickTexture.Width, 200), 0, 2, 0, false);
            }
            for (int i = 0; i < 14; i++)
            {
                //Draw left side brick walls
                AddObjectToWorld(new LevelObject(this, brickTexture), new Vector2(900, 200 + (i * brickTexture.Width)), 0, 2, 0, false);
                AddObjectToWorld(new LevelObject(this, brickTexture), new Vector2(900+brickTexture.Height, 200 + (i * brickTexture.Height)), 0, 2, 0, false);
                
                //Draw right side
                AddObjectToWorld(new LevelObject(this, brickTexture), new Vector2(900 + (13 * brickTexture.Width), 200 + (i * brickTexture.Width)), 0, 2, 0, false);
                AddObjectToWorld(new LevelObject(this, brickTexture), new Vector2(900 + (13 * brickTexture.Width) + brickTexture.Width, 200 + (i * brickTexture.Height)), 0, 1, 0, false);
            }

            for (int i = 1; i <= 13; i++)
            {
                AddObjectToWorld(new LevelObject(this, brickTexture), new Vector2(900 + (i * brickTexture.Width), 1110), 0, 2, 0, false);
            }


            //Fire ze turretz
            for (int i = 0; i < 5; i++)
            {
                float j = i % 2;
                AddObjectToWorld(new StaticTurret(this, 2+j*0.5f, true, true, 2), new Vector2(1030, 200 + (i * 200)), 0, 1, 0, false);
            }

            for (int i = 0; i < 4; i++)
            {
                float j = i % 2;
                AddObjectToWorld(new StaticTurret(this, 2 + j * 0.5f, false, false, 2), new Vector2(1750, 300 + (i * 200)), 0, 1, 0, false);
            }

            //Ze locks
            Texture2D lockTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/lockBlock_horizontal");
            for (int i = 0; i < 8; i++)
            {
                AddObjectToWorld(new Door(this, 1, lockTexture), new Vector2(1030 + i * lockTexture.Width, 1036), 0, 1, 0, false);
            }
            
            //zekey
            AddObjectToWorld(new Key(this, doors, 1), new Vector2(1300, 620), 0, 1, 50, false);


            Collectable collectable = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable, new Vector2(450, 200), 0, 2, 3);

            LevelObject start = new Warp(this, true, startTexture);
            AddObjectToWorld(start, new Vector2(100, 100), 0, 1, 0, true);

            LevelObject end = new Warp(this, false, endTexture);
            AddObjectToWorld(end, new Vector2(1750, 1060), 0, 1, 10, false);


            //Setup positions

            _startPos = start.Body.Position;
            player.Position = _startPos;

            //ground.Body.Position = ConvertUnits.ToSimUnits(new Vector2(screenWidth / 2, 500));  
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (isDead)
            {
                isDead = false;
                LoadContent();
            }
        }

        public override PhysicsGameScreen NextLevel()
        {
            return new Level3();
        }
    }
}
